package vn.team.clipxonxao.ui;

import java.util.ArrayList;
import java.util.List;

import vn.team.clipxonxao.adapter.HomeClipAdaper;
import vn.team.clipxonxao.model.VideoClip;
import vn.team.clipxonxao.model.VideoClipList;
import vn.team.clipxonxao.request.DetailPageRequest;
import vn.team.clipxonxao.request.TotalPageRequest;
import vn.team.clipxonxao.respone.DetailPageRespone;
import vn.team.clipxonxao.respone.TotalPageRespone;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.mk.fw.connection.APIResponse;
import com.mk.fw.connection.ResponseReceiver;
import com.mk.fw.imageloader.MKImageFetcher;
import com.mk.fw.ui.MKFragment;

public class HomeClipXonXao extends MKFragment implements ResponseReceiver{

	private View mView;
	private ListView mListView;
	private View mProgressBar;
	private List<VideoClip> listVideo;
	private HomeClipAdaper adapter;
	private VideoClipList objectListVideo;
	private final int LOADER_TOTALPAGE = 102;
	private final int LOADER_DETAILPAGE = 103;
	private final int NUMBER_FIRST = 1;
	private int page = NUMBER_FIRST;
	private int totalPage;
	private int idCategory;
	private MKImageFetcher imageLoader;
	private FragmentActivity mContext;
	private String nameTitle;
	private View mViewFooter;
    private LinearLayout layoutFooterProgress;
    private LinearLayout layoutFooterDisconnection;
//    private Button btnRetryFooterList;
    private LayoutInflater inflater;
    private boolean isLoadingMore = false;
	
	public HomeClipXonXao(){
		
	}
	
	public HomeClipXonXao(int idCategory, String nameTitle){
		this.idCategory =  idCategory;
		this.nameTitle = nameTitle;
	}
	
	private void initialView(View view){
		mListView = (ListView)view.findViewById(R.id.layout_content_fragment_listVideo);
		mProgressBar = (View)view.findViewById(R.id.layout_content_fragment_ProgressBar);
		listVideo = new ArrayList<VideoClip>();
		mContext = getActivity();
		inflater = LayoutInflater.from(mContext);
		mViewFooter = inflater.inflate(R.layout.layout_footer_progress_bar, null);
    	layoutFooterProgress = (LinearLayout)mViewFooter.findViewById(R.id.layoutLoadingFooterList);
    	layoutFooterDisconnection = (LinearLayout)mViewFooter.findViewById(R.id.layoutDisconnectFooterList);
//    	btnRetryFooterList = (Button)mViewFooter.findViewById(R.id.btnRetryFooterList);
		mListView.addFooterView(mViewFooter, null, false);
		AppMain.setTitleName(nameTitle);
		imageLoader = new MKImageFetcher(mContext, getFragmentManager(), 75, 55);

		adapter = new HomeClipAdaper(mContext, listVideo, imageLoader);
		mListView.setAdapter(adapter);
		requestGetTotalPage();
		requestGetData();
		mListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if(!isLoadingMore  && page < totalPage && totalItemCount >0 && (firstVisibleItem + visibleItemCount) == totalItemCount){
					layoutFooterDisconnection.setVisibility(View.GONE);
					requestGetData();
					layoutFooterProgress.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
			}
			
		});
	}
	
	private void requestGetTotalPage(){
		restartRequestServer(LOADER_TOTALPAGE, new TotalPageRequest(idCategory), this);
	}
	
	private void requestGetData(){
		restartRequestServer(LOADER_DETAILPAGE, new DetailPageRequest(idCategory, page), this);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.layout_content_fragment, null, false);
		initialView(mView);
		return mView;
	}

	@Override
	public void onBaseLoaderReset(Loader<APIResponse> loader) {
		
	}

	@Override
	public void receiveResponse(Loader<APIResponse> loader, APIResponse response) {
		if(response!= null){
			int loaderId = loader.getId();
			switch (loaderId) {
				case LOADER_TOTALPAGE:
					TotalPageRespone totalPageRespone = (TotalPageRespone) response;
					handleTotalPageResponse(totalPageRespone);
					break;
				case LOADER_DETAILPAGE:
					isLoadingMore = true;
					DetailPageRespone detailPageRespone = (DetailPageRespone) response;
					handleDetailPageResponse(detailPageRespone);
					break;
			}
		}
		// Comment tam thoi
		else{
			if(page > NUMBER_FIRST){
				page = page - 1;
			}
			if(mProgressBar!=null){
				mProgressBar.setVisibility(View.GONE);
			}
		}
	}
	
	
	private void handleDetailPageResponse(DetailPageRespone detailPageRespone){
		try {
			objectListVideo = detailPageRespone.getListCategory();
			if (page == 1) {
				listVideo.addAll(objectListVideo.getListCategory());
				if(mProgressBar!=null){
					mProgressBar.setVisibility(View.GONE);
				}
				if(totalPage > page){
					page = page + NUMBER_FIRST;
				}
			}
			else if(page >1 ){
				listVideo.addAll(objectListVideo.getListCategory());
				if(totalPage > page){
					page = page + NUMBER_FIRST;
				}
				if(layoutFooterProgress!= null){
					layoutFooterProgress.setVisibility(View.GONE);
				}
			}
			isLoadingMore = false;
			adapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private void handleTotalPageResponse(TotalPageRespone totalPageRespone) {
		totalPage = totalPageRespone.getTotalPage();
		Log.e("totalPage first",""+totalPage);
	}
	
}
